﻿Public Class UCGQuartArriere
    Public Sub charger()
        For Each employe In bd.Intervenants
            Dim item As New ListViewItem
            item.Text = employe.Employe.Nom + ", " + employe.Employe.Prenom
            item.Tag = employe.IDEmploye
            LVEmployés.Items.Add(item)
        Next

        For Each employe In (From i In bd.Intervenants Where Not i.PrioriteQuart = -1 Order By i.PrioriteQuart Select i)
            Dim item As New ListViewItem
            item.Text = employe.Employe.Nom + ", " + employe.Employe.Prenom
            item.Tag = employe.IDEmploye
            LVQuartArriere.Items.Add(item)
        Next
    End Sub

    Private Sub BVerQuart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BVerQuart.Click
        If LVEmployés.SelectedItems.Count > 0 Then
            Dim deja = From i As ListViewItem In LVQuartArriere.Items Where i.Tag = LVEmployés.SelectedItems(0).Tag Select i

            If deja.Count = 0 Then
                LVQuartArriere.Items.Add(LVEmployés.SelectedItems(0).Clone)
            End If
        End If
    End Sub

    Private Sub BVEmploye_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BVEmploye.Click
        If LVQuartArriere.SelectedItems.Count > 0 Then
            LVQuartArriere.Items.Remove(LVQuartArriere.SelectedItems(0))
        End If
    End Sub

    Private Sub Bplus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bplus.Click
        If LVQuartArriere.SelectedItems.Count > 0 Then
            Dim currentIndex As Integer = LVQuartArriere.SelectedItems(0).Index
            Dim item = LVQuartArriere.SelectedItems(0)
            If (currentIndex > 0) Then
                LVQuartArriere.Items.RemoveAt(currentIndex)
                LVQuartArriere.Items.Insert(currentIndex - 1, item)
            End If
        End If
    End Sub

    Private Sub BMoin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BMoin.Click
        Dim currentIndex As Integer = LVQuartArriere.SelectedItems(0).Index
        Dim item = LVQuartArriere.SelectedItems(0)
        If (currentIndex < LVQuartArriere.Items.Count - 1) Then
            LVQuartArriere.Items.RemoveAt(currentIndex)
            LVQuartArriere.Items.Insert(currentIndex + 1, item)
        End If
    End Sub

    Public Sub EnregistrerQuartArriere()
        For Each employe In (From i In bd.Intervenants Where Not (From x As ListViewItem In LVQuartArriere.Items Select x.Tag).Contains(i.IDEmploye) Select i)
            employe.PrioriteQuart = -1
        Next
        Dim pos As Integer = 0
        For Each items In (From i As ListViewItem In LVQuartArriere.Items Order By i.Index Select i)
            Dim item = items
            Dim employe = (From i In bd.Intervenants Where i.IDEmploye = Convert.ToInt32(item.Tag) Select i).First
            employe.PrioriteQuart = pos
            pos += 1
        Next
        bd.SubmitChanges()
    End Sub
End Class
